package ysq.xyg.host.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostItemSupplementDao;
import ysq.xyg.host.model.po.HostItemSupplement;
import ysq.xyg.host.model.vo.HostItemSupplementVo;
import ysq.xyg.host.service.HostItemSupplementService;
import ysq.xyg.sys.model.po.SysUser;

import java.util.Date;
import java.util.List;

/**
 * 房产-楼盘补充信息service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:17:59
 */
@Service("hostItemSupplementService")
public class HostItemSupplementServiceImpl extends BaseServiceImpl<HostItemSupplement> implements HostItemSupplementService {

    @Autowired
    private HostItemSupplementDao hostItemSupplementDao;

    @Autowired
    public void setBaseDao(HostItemSupplementDao hostItemSupplementDao) {
        this.baseDao = hostItemSupplementDao;
    }

    /**
     * 分页查询
     *
     * @param hostItemSupplementVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:17:59
     */
    @Override
    public ReturnEntity page(HostItemSupplementVo hostItemSupplementVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" create_time desc");
        List<HostItemSupplement> list = acceptList(hostItemSupplementVo);
        PageInfo<HostItemSupplement> page = new PageInfo<HostItemSupplement>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostItemSupplementVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:17:59
     */
    @Override
    public ReturnEntity addOrUpdate(HostItemSupplementVo hostItemSupplementVo, SysUser currentUser) {
        boolean isUpate = true;
        HostItemSupplement hostItemSupplement;
        if (StrUtil.isEmpty(hostItemSupplementVo.getId())) {
            hostItemSupplement = new HostItemSupplement(currentUser);
            isUpate = false;
        } else {
            hostItemSupplement = selectByPrimaryKey(hostItemSupplementVo.getId());
            if (ObjectUtil.isNull(hostItemSupplement)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostItemSupplement.setUpdateTime(new Date());
            hostItemSupplement.setUpdateUser(currentUser.getId());
        }
        hostItemSupplement.setItemId(hostItemSupplementVo.getItemId());
        hostItemSupplement.setDescription(hostItemSupplementVo.getDescription());
        hostItemSupplement.setImage(hostItemSupplementVo.getImage());
        hostItemSupplement.setCertificate(hostItemSupplementVo.getCertificate());
        hostItemSupplement.setContent(hostItemSupplementVo.getContent());
        hostItemSupplement.setOrderNum(hostItemSupplementVo.getOrderNum());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostItemSupplement);
        } else {
            num = insert(hostItemSupplement);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:17:59
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostItemSupplement hostItemSupplement = selectByPrimaryKey(id);
        if (hostItemSupplement != null) {
            hostItemSupplement.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostItemSupplement.setUpdateTime(new Date());
            hostItemSupplement.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostItemSupplement);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostItemSupplementVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:17:59
     */
    @Override
    public List<HostItemSupplement> acceptList(HostItemSupplementVo hostItemSupplementVo){
        Example example = new Example(HostItemSupplement.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostItemSupplementVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostItemSupplementVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostItemSupplementVo.getItemId())) {
            criteria.andEqualTo("itemId", hostItemSupplementVo.getItemId());
        }
        example.setOrderByClause(" create_time desc");
        return hostItemSupplementDao.selectByExample(example);
    }

    @Override
    public HostItemSupplement selectOneByItemId(String itemId) {
        HostItemSupplementVo hostItemSupplementVo = new HostItemSupplementVo();
        hostItemSupplementVo.setItemId(itemId);
        List<HostItemSupplement> hostItemSupplements = acceptList(hostItemSupplementVo);
        if (CollUtil.isNotEmpty(hostItemSupplements)) {
            return hostItemSupplements.get(0);
        }
        return null;
    }
}
